<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增AI Agent应用"
    width="50%"
    :before-close="handleClose"
  >
    <el-form :model="form" label-width="120px">
      <el-form-item label="应用名称" required>
        <el-input v-model="form.name" placeholder="请输入应用名称"></el-input>
      </el-form-item>
      <el-form-item label="应用描述" required>
        <el-input
          v-model="form.description"
          type="textarea"
          placeholder="请输入应用描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="应用图标" required>
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :before-upload="beforeAvatarUpload"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="form.icon" :src="form.icon" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="系统提示词" required>
        <el-input
          v-model="form.systemPrompt"
          type="textarea"
          :rows="4"
          placeholder="请输入系统提示词"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'AddAgentDialog',
  components: {
    Plus
  },
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:modelValue', 'submit'],
  data() {
    return {
      form: {
        name: '',
        description: '',
        icon: '',
        systemPrompt: ''
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.modelValue
      },
      set(value) {
        this.$emit('update:modelValue', value)
      }
    }
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
      this.resetForm()
    },
    resetForm() {
      this.form = {
        name: '',
        description: '',
        icon: '',
        systemPrompt: ''
      }
    },
    beforeAvatarUpload(file) {
      const isImage = file.type.startsWith('image/')
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isImage) {
        ElMessage.error('上传图标只能是图片格式!')
        return false
      }
      if (!isLt2M) {
        ElMessage.error('上传图标大小不能超过 2MB!')
        return false
      }

      // 将文件转换为Base64
      return new Promise((resolve) => {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          this.form.icon = reader.result
          resolve(false) // 阻止默认上传
        }
      })
    },
    handleAvatarSuccess(res, file) {
      // 由于我们在beforeAvatarUpload中已经处理了图片，这里不需要做任何事情
    },
    handleSubmit() {
      if (!this.form.name || !this.form.description || !this.form.icon || !this.form.systemPrompt) {
        ElMessage.error('请填写所有必填项！')
        return
      }

      this.$emit('submit', { ...this.form })
      this.handleClose()
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader:hover {
  border-color: var(--el-color-primary);
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>